#include <bits/stdc++.h>
using namespace std;
const int maxn = 64+5;
bool G[maxn][maxn];
int N, T = 0;
vector<int> seq;

void getSeq(int x, int y, int w, string s){
    bool fir = G[x][y]; bool dif = false;
    for (int r = x; r < x+w; ++r) {
        for (int c = y; c < y+w; ++c) {
            if(fir!=G[r][c]) {dif = true; break;}
        }
        if(dif) break;
    }
    if(dif){
        getSeq(x, y, w/2, s+"1");
        getSeq(x, y+w/2, w/2, s+"2");
        getSeq(x+w/2, y, w/2, s+"3");
        getSeq(x+w/2, y+w/2, w/2, s+"4");
    }else{
        if(fir){
            if(s.empty()) seq.push_back(0);
            else{
                int dec = 0;
                for (int i = 0; i < s.size(); ++i) {
                    dec += (s[i]-'0') * (int)pow(5, i);
                }
                seq.push_back(dec);
            }
        }
    }
}

void build(){
    vector<string> five;
    if(seq.empty()) memset(G, false, sizeof(G));
    else if(!seq[0]) memset(G, true, sizeof(G));
    else{
        for (const int &i:seq){
            string s; int t = i;
            while(t/5){
                s.push_back(t%5+'0');
                t /= 5;
            }
            if(t)s.push_back(t+'0');
            five.push_back(s);
        }

        for (string &s:five){
            int r=1, c=1, w=N;
            for (char &ch:s){
                w = w>>1;
                if(ch == '2') c+=w;
                if(ch == '3') r+=w;
                if(ch == '4') {r+=w; c+=w;}
            }
            for (int i = r; i < r+w; ++i) {
                for (int j = c; j < c+w; ++j) {
                    G[i][j] = true;
                }
            }
        }
    }

    
}
int main() {
    //ios_base::sync_with_stdio(false);cin.tie(nullptr);
    while (cin >> N && N){
        if(++T>1) cout << "\n";
        cout << "Image " << T << "\n";
        memset(G, false, sizeof(G));
        seq.clear();
        if(N>0){
            for (int r = 1; r <= N; ++r) {
                string in; cin >> in;
                for (int c = 1; c <= N; ++c) {
                    if(in[c-1] == '1') G[r][c] = true;
                }
            }
            getSeq(1, 1, N, "");
            sort(seq.begin(), seq.end());
            for (int i = 1; i <= seq.size(); ++i) {
                if(i%12!=1) cout << " ";
                cout << seq[i-1];
                if(i%12==0) cout << "\n";
            }
            if(seq.size()%12) cout << "\n";
            cout << "Total number of black nodes = " << seq.size() << "\n";
        }else{
            N = -N;
            int in;
            while(cin >> in && in!=-1) seq.push_back(in);
            build();
            for (int r = 1; r <= N; ++r) {
                for (int c = 1; c <= N; ++c) {
                    char ch = G[r][c]? '*':'.';
                    cout << ch;
                }
                cout << "\n";
            }
        }
    }
    return 0;
}